package cn.thr.pethome.pet.service.impl;

import cn.thr.pethome.constants.BaseConstants;
import cn.thr.pethome.jwt.LoginContextUtils;
import cn.thr.pethome.pet.domain.SearchMasterMsg;
import cn.thr.pethome.pet.domain.SearchMasterMsgAuditLog;
import cn.thr.pethome.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.thr.pethome.pet.mapper.SearchMasterMsgMapper;
import cn.thr.pethome.pet.service.ISearchMasterMsgService;
import cn.thr.pethome.base.service.Impl.BaseServiceImpl;
import cn.thr.pethome.shop.domain.Shop;
import cn.thr.pethome.shop.mapper.ShopMapper;
import cn.thr.pethome.user.domain.User;
import cn.thr.pethome.util.AuthService;
import cn.thr.pethome.util.BaiduMapUtils;
import cn.thr.pethome.util.BusinessException;
import cn.thr.pethome.util.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author thr
 * @since 2023-02-08
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private LoginContextUtils loginContextUtils; // 用来获取发布人的信息

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private AuthService authService; //百度审核

    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

    @Autowired
    private ShopMapper shopMapper;
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        //百度审核名字
        if(!authService.authentication(searchMasterMsg.getTitle())){
            throw new BusinessException("你的名字有问题");
        }
        //创建审核表
        SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
        //审核备注
        searchMasterMsgAuditLog.setNote("审核通过");
        //审核时间
        searchMasterMsgAuditLog.setAuditTime(new Date());
        //状态
        searchMasterMsgAuditLog.setState(BaseConstants.publish.STATE_PASS);

        //店铺 计算店铺地址
        Point point = BaiduMapUtils.getPoint(searchMasterMsg.getAddress());//获取发布者的地址

        List<Shop> collect = shopMapper.findAll().stream()
                .filter(shop -> {//过滤在50之类的
                    Point point1 = BaiduMapUtils.getPoint(shop.getAddress());//获取店铺的地址
                    return BaiduMapUtils.getDistance(point, point1) < 50000;  //返回在5公里之内的店铺店铺推送出去
                }).collect(Collectors.toList());

        if(!collect.isEmpty()){ //如果是空就是true
            Double min = 50000.0; //最小 5公里
            Long shopId = null;
            for (Shop shop : collect) {
                double distance = BaiduMapUtils.getDistance(point, BaiduMapUtils.getPoint(shop.getAddress()));

                if(distance <min){ //只要店铺中，在5公里之内的店都推送出去
                    min = distance;
                    shopId = shop.getId(); //告诉5公里内的店铺id传进去
                }
                searchMasterMsg.setShopId(shopId);
            }
        }

        //获取到用户的信息
        User loginUser = (User) loginContextUtils.getLoginUser(request);
        //知道发消息的用户是谁 拿到用户的id
        searchMasterMsg.setUserId(loginUser.getId());
        //状态
        searchMasterMsg.setState(BaseConstants.publish.STATE_PASS);

        //把数据添加进去
        searchMasterMsgMapper.add(searchMasterMsg);
        //这个是关联寻主消息的  把数据全部添加回去
        searchMasterMsgAuditLog.setMsgId(searchMasterMsg.getId());
        searchMasterMsgAuditLogMapper.add(searchMasterMsgAuditLog);

    }
}
